<?php
/**
 * 微校云预定控制器
 * Author: 孙武 QQ:1228746736
 * Date: 2017/10/28
 * Time: 20:17
 */


namespace Home\Controller;

use Home\Model\IndexModel;
use Home\Model\PreOrderModel;

class PreOrderController extends BaseOrderController
{

    private $_M;//模型

    public function __construct()
    {
        parent::__construct();
        $this->_M = new PreOrderModel();
    }

    /**
     * 预定票页面
     */
    public function preBookTicket()
    {

        if (!empty($_POST)) {

            $selectedPsrPostData = $_POST['checkbox'];
            //选中的用户的id
            $psrIds = array();
            foreach ($selectedPsrPostData as $k => $v) {
                $psrIds[] = $k;
            }
            //数组转换为字符串
            $psrIds = implode(',', $psrIds);

            //根据选中用户的id，获取用户的显示信息
            $sql = "select * from bus_passenger where psr_id in ($psrIds)";
            $psrIds = D()->query($sql);
            $this->assign("psrInfo", $psrIds);

        }

        //处理车票信息
        if ($_GET['ticketId']) {

            //session持久化票据

            session('ticketId', I('get.ticketId'));

            $ticketInfo = D('addr')->find(session('ticketId'));

//           处理时间戳
            $ticketInfo['addr_depart_time'] = date("Y-m-d H:i", $ticketInfo['addr_depart_time']) . " " . changeNumberToweekday(date('w', $ticketInfo['addr_depart_time']));
            $this->assign("ti", $ticketInfo);
        }
        $this->assign("tickedId", session('ticketId'));
        $this->assign("insur", 3);
        $this->display('preBookTicket');
    }




    /**
     * 预定页面
     */
    public function prePayTicket()
    {


        //车票id
        $_ticketId = session('ticketId');


        //乘客数组
        $psrIds = $_POST['psr'];

        //乘客数量
        $_psrNum = count($psrIds);

        $_ticketInfo = $this->getAddrInfoById(session('ticketId'));

        //单票价
        $unitPrice = $_ticketInfo['addr_price'];


        //根据折扣code获取折扣id
        $dicCode = $_POST['dicCode'];
        $map = array('dc_code' => $dicCode);
        $discountInfo = D('discount')->where($map)->find();


        //保存到数据库信息
        $dbData = array(
            'user_id' => session('userId'),
            'od_name' => $this->getOrderId(),
            'od_creat_time' => time(),
            'od_finish_time' => '0',
            'od_ticket_num' => $_psrNum,
            "od_total_price" => $this->getTotalPrice(),
            'od_sell_price' => $this->getSellPrice($discountInfo),
            'od_status' => '0',
            'od_psr_ids' => $this->getPsrIds($psrIds),
            'od_addr_id' => $_ticketId,
            'od_insur_price' => $this->getInsurPrice($_psrNum),
            'od_discount_id' => $discountInfo['dc_id'],
            'od_get_ticket_person' => $_POST['get_ticket_pseson'],
            'od_get_ticket_person_tel' => $_POST['get_ticket_tel'],
            'od_pay_status' => '0',

        );

        //检查条件
        if (($errMsg = $this->checkIsPay($dbData)) !== true) {
            $this->err('非法操作', $errMsg);
            exit;
        }

        //保存数据库
        if (!$orderId = D('pre_order')->add($dbData)) {
            die('未知错误,请重试!');
        }


        //给模板传值
        //单价
        $this->assign("unitPrice", $unitPrice);
        //总价
        $this->assign("totalPrice", $this->getTotalPrice());
        //折后价
        $this->assign("sellPrice", $this->getSellPrice($discountInfo));
        //出发地,目的地,时间
        $addrInfo = $this->getAddrInfoById($_ticketId);
        $this->assign("from", $addrInfo['addr_from']);
        $this->assign("to", $addrInfo['addr_to']);
        $this->assign("depart_time", $this->getFormateDateFromTimestamp($addrInfo['addr_depart_time']));
        //乘车人
        $psrInfo = $this->getPsrInfoByPsrIds($psrIds);
        $this->assign("psrInfo", $psrInfo);
        //取票人 取票人电话
        $this->assign("getTicketPersonName", $_POST['get_ticket_pseson']);
        $this->assign("getTicketPersonTel", $_POST['get_ticket_tel']);
        //保险费
        $this->assign("insurPrice", $this->getInsurPrice($_psrNum));


        $this->assign("orderId", $orderId);
        $this->display();


    }




}